package com.by.hellosecurityresource.service.impl;

import com.by.hellosecurityresource.domain.STest;
import com.by.hellosecurityresource.domain.repository.STestRepository;
import com.by.hellosecurityresource.service.TestService;
import com.by.hellosecurityresource.controller.request.TestRequestBean;

import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaUpdate;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.Date;
import java.util.List;

/**
 * @author 白羽
 * @Date 2022-12-12 12:56:51
 */
@Service
@Transactional(value = "transactionManager", isolation = Isolation.REPEATABLE_READ,
        propagation = Propagation.REQUIRED)
public class TestServiceImpl implements TestService {

    private final STestRepository testRepository;
    
    public TestServiceImpl(STestRepository testRepository) {
        this.testRepository = testRepository;
    }
    
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public STest findById(Long id) {
        return testRepository.findById(id).orElse(null);
    }

    /**
     * 查询多条数据
     *
     * @param page 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override 
    public Page<STest> list(int page, int limit) {
        Pageable pageable = PageRequest.of(page, limit, Sort.by(Sort.Direction.DESC, "id"));
        return testRepository.findAll((Specification<STest>) (root, criteriaQuery, criteriaBuilder) -> {
            Predicate predicate = criteriaBuilder.conjunction();

            return predicate;
        }, pageable);
    }

    /**
     * 新增数据
     *
     * @param bean 实例对象
     * @return 实例对象
     */
    @Override
    public STest create(TestRequestBean bean) {
        STest test = new STest();
        test.setTest(bean.getTest());
        test.setValue(bean.getValue());
        return testRepository.save(test);
    }

    /**
     * 修改数据
     * @param id 主键
     * @param bean 实例对象
     * @return 实例对象
     */
    @Override
    public STest updateById(Long id, TestRequestBean bean) {
        STest test = findById(id);
        test.setTest(bean.getTest());
        test.setValue(bean.getValue());
        return testRepository.save(test);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override 
    public void deleteById(Long id) {
        testRepository.deleteById(id);
    }
    
}